<?php
defined('SMARTY_DIR') or die( 'Restricted access' );

        function checkUser($login,$password){
                $res = mysql_query("SELECT u_login,u_password FROM users");
                while($row=mysql_fetch_array($res)){
                        if($row['u_login']==$login && $row['u_password']==$password){
                                return true;
                        }
                }
                return false;
        }
        
        
        function registerToDB($name,$surname,$faculty,$group,$email,$login,$password){    
        	$pass=md5($password);
                 $time=time();
                 $res = mysql_query("INSERT INTO users(u_name,u_surname,group_id,u_email,u_login,u_password,u_registerdate)
                  VALUES('$name','$surname','$group','$email','$login','$pass',now())") or die(mysql_error());
                
                  return true;
        }

        function selectSubject($subject_id){
        
                
        }
        
        function checkRegistration(){
                
                if(isset($_SESSION['u_login'])) return true;
                
                return false;
        }


		function getCategories()
		{
			$c = array(array(),array());
			$sql = "select * from categories ";
			if($_SESSION["fselected"]) $sql.="WHERE faculty_ids LIKE '%".$_SESSION['fselected']."%' ";
			//echo $sql;
			$res = mysql_query($sql);
			while($row=mysql_fetch_array($res))
			{	
				$c[0][] = $row["cat_id"];
				$c[1][]= $row["cat_title"];
			}
			return $c;
		}

		
		
function showStatistics($smarty,$result){
				
	$date=array();
	$subject=array();
	if($result==1){
	$query = mysql_query("SELECT correct_answers AS cor_ans,cat_id,test_taken_date FROM results WHERE u_id=".$_SESSION['user_id']);
	}else if($result==2){
	$query = mysql_query("SELECT correct_answers AS cor_ans,cat_id,test_taken_date FROM results WHERE u_id=".$_SESSION['user_id']." ORDER BY correct_answers DESC LIMIT 0 , 1");
	}else if($result==3){
	$query = mysql_query("SELECT AVG(correct_answers) AS cor_ans ,cat_id,test_taken_date FROM results WHERE u_id=".$_SESSION['user_id']." GROUP BY cat_id");
	}
	
	
	
	$num = mysql_num_rows($query);
	
	$res=array();
	$query2 = mysql_query("SELECT * FROM users WHERE id=".$_SESSION['user_id']);

	for($i=0;$i<$num;$i++){
		$row2 = mysql_fetch_array($query2);
		$res[$i][3]=$row2['u_name'];
		
		$res[$i][4]=$row2['u_surname'];
	}
	$cat_id=array();
	$cat_id[0]=0;
	$subject[0]="Subject";
	$date_id=array();
	for($i=0;$i<$num;$i++){
		$row = mysql_fetch_array($query);
		$res[$i][1]=$row['cor_ans'];
		$query4=mysql_query("SELECT * FROM categories WHERE cat_id=".$row['cat_id']);
		$cat_id[$i+1]=$row['cat_id'];
		$rowx = mysql_fetch_array($query4);
		$res[$i][0]=$rowx['cat_title'];
		$res[$i][2]=$row['test_taken_date'];
		$date[$i]=$row['test_taken_date'];
		$date_id[$i]=$i;
	}
	
	//$sub_query = mysql_query("SELECT DISTINCT ");
	
////////////////Astindagi Resulttar bitti	
	$smarty->assign("cat_id",$cat_id);
	$smarty->assign("cat_name",$subject);
	$smarty->assign("date",$date);
	$smarty->assign("date_id",$date_id);
	$smarty->assign("rownum",$num);
	$smarty->assign("cor_ans",$res);	
}
?>